****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
**By Andrew Bibler & Stephen B Billings
****************************************************************************
		
clear all
set more off

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"

*use schlcode_avgscores_cms.dta, clear

use schlcode_avgchars_cms.dta , clear

keep mathstand readstand schlcode

rename mathstand mathstand_hs 
rename readstand readstand_hs
rename schlcode homeschl

tempfile hsscores 
	save `hsscores'

use all-lottery-movers-analysis.dta , clear 

keep if (year>2008 & ksample & stayer==1)

merge m:1 homeschl using `hsscores' , gen(mergehsscores)

keep if (mergehsscores==1 | mergehsscores==3)

gen mathstand_diff = mathstand_as - mathstand_at
gen readstand_diff = readstand_as - readstand_at

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)

gen attappch1schl = (curr_lead1==appch1school)

gen attassschl = (curr_lead1==assschl)

gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier

gen move1_win_noncomp = win_noncomplier*move1
gen move1_win_comp = win_complier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen move1_win_noncomp_sc = move1_win_noncomp*avgscore_diff
gen move1_win_comp_sc = move1_win_comp*avgscore_diff
gen move1_lost_sc = move1_lost*avgscore_diff

egen tercma = xtile(mathstand_hs) , n(3)
gen lowhsma = (tercma==1)
gen medhsma = (tercma==2)
gen highhsma = (tercma==3)

egen tercrd = xtile(readstand_hs) , n(3)
gen lowhsrd = (tercrd==1)
gen medhsrd = (tercrd==2)
gen highhsrd = (tercrd==3)

gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
egen tercavgscorehs = xtile(avgscores_hs) , n(3)
gen lowavgscorehs = (tercavgscorehs==1)
gen medavgscorehs = (tercavgscorehs==2)
gen highavgscorehs = (tercavgscorehs==3)

**egen terchp = xtile(Elem_price_mean_hs) , n(3)
egen terchp = xtile(Elem_price_residV2_hs) , n(3)
gen lowhsprice = (terchp==1)
gen medhsprice = (terchp==2)
gen highhsprice = (terchp==3)	

egen terchpv2 = xtile(Elem_price_residV2_hs) , n(3)
gen lowhspricev2 = (terchp==1)
gen medhspricev2 = (terchp==2)
gen highhspricev2 = (terchp==3)	
	

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if (lunch==1) , fe cluster(lottery) 

estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store A

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if (lunch==0) , fe cluster(lottery) 

estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store B

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if male==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store C

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if male==0 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store D

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if white==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store E

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if white==0 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store F

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if lowavgscorehs==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store G

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if medavgscorehs==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store H

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if highavgscorehs==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store I

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if lowhsprice==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store J

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if medhsprice==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store K

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if highhsprice==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store L


xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store main

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if appch1magnet==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store A2

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if appch1dl==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store B2

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if appch1trad==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store C2

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if winprob<0.5 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store D2

xtreg elem_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons if winprob>=0.5 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store E2

cd "\\syn.iseralaska.org\hppc-data\NCERDC\movingprob\ReStat-replication-files"

** table A17
esttab A B C D E F G H I J K L using tableA17.tex , replace ///
mtitles("Yes" "No" "Male" "Female" "White" "Non-white" "Low" "Middle" "High" "Low" "Middle" "High") ///
refcat(move1_lost_sc "\emph{Change HS X Score Diff}" , nolabel ) ///
keep(move1_lost_sc move1_win_noncomp_sc) ///
coef(move1_lost_sc "Lost" move1_win_noncomp_sc "Won X Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E F G H I J K L using tableA17.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(cbgcontrols N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"1st Stage CBG Controls"' `"Observations"'))

** table A19
esttab main A2 B2 C2 D2 E2 using tableA19.tex , replace ///
mtitles("Full Sample" "Magnet" "DL" "Trad." "<0.5" ">=0.5") ///
refcat(move1_lost_sc "\emph{Change HS X Score Diff}" , nolabel ) ///
keep(move1_lost_sc move1_win_noncomp_sc) ///
coef(move1_lost_sc "Lost" move1_win_noncomp_sc "Won X Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab main A2 B2 C2 D2 E2 using tableA19.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(cbgcontrols N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"1st Stage CBG Controls"' `"Observations"'))


** sixth grade

use stay-lottery-movers-analysis.dta , clear
keep if sixsample==1

merge m:1 homeschl using `hsscores' , gen(mergehsscores)
keep if (mergehsscores==1 | mergehsscores==3)

gen mathstand_diff = mathstand_as - mathstand_at
gen readstand_diff = readstand_as - readstand_at

gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen move1_win_comp = win_complier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1
gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as
gen move1_win_noncomp_sc = move1_win_noncomp*avgscore_diff
gen move1_win_comp_sc = move1_win_comp*avgscore_diff
gen move1_lost_sc = move1_lost*avgscore_diff

replace midbyCBG_price_resid_diff = mid_price_resid_diff if (midbyCBG_price_resid_diff==. | move1==0)
replace midbyCBG_price_residV2_diff = mid_price_residV2_diff if (midbyCBG_price_residV2_diff==. | move1==0)

egen tercma = xtile(mathstand_hs) , n(3)
gen lowhsma = (tercma==1)
gen medhsma = (tercma==2)
gen highhsma = (tercma==3)

egen tercrd = xtile(readstand_hs) , n(3)
gen lowhsrd = (tercrd==1)
gen medhsrd = (tercrd==2)
gen highhsrd = (tercrd==3)

gen avgscores_hs = (0.5*mathstand_hs + 0.5*readstand_hs)
egen tercavgscorehs = xtile(avgscores_hs) , n(3)
gen lowavgscorehs = (tercavgscorehs==1)
gen medavgscorehs = (tercavgscorehs==2)
gen highavgscorehs = (tercavgscorehs==3)

*egen terchp = xtile(Mid_price_mean_hs) , n(3)
egen terchp = xtile(Mid_price_residV2_hs) , n(3)
gen lowhsprice = (terchp==1)
gen medhsprice = (terchp==2)
gen highhsprice = (terchp==3)	

egen terchpv2 = xtile(Mid_price_residV2_hs) , n(3)
gen lowhspricev2 = (terchp==1)
gen medhspricev2 = (terchp==2)
gen highhspricev2 = (terchp==3)	
	
egen tercmath = xtile(math_lag) , n(3)
gen lowmath = (tercmath==1)
gen medmath = (tercmath==2)
gen highmath = (tercmath==3)

egen tercread = xtile(read_lag) , n(3)
gen lowread = (tercread==1)
gen medread = (tercread==2)
gen highread = (tercread==3)

gen avgmathread_lag = (0.5*math_lag + 0.5*read_lag)
egen tercmardavg = xtile(avgmathread_lag) , n(2)
gen lowavg = (tercmardavg==1)
gen highavg = (tercmardavg==2)

xtset lottery

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (lunch==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store A

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (lunch==0) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store B

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (male==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store C

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (male==0) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store D

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (white==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store E

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (white==0) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store F

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (lowavgscorehs==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store G

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (medavgscorehs==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store H

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (highavgscorehs==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store I

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (lowhsprice==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store J

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (medhsprice==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store K

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (highhsprice==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store L

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (lowavg==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store M

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons if (highavg==1) , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store O

cd "\\syn.iseralaska.org\hppc-data\NCERDC\movingprob\ReStat-replication-files"

** table A18
esttab A B C D E F G H I J K L M O using tableA18.tex , replace ///
mtitles("Yes" "No" "Male" "Female" "White" "Non-white" "Low" "Middle" "High" "Low" "Middle" "High" "Low" "High") ///
refcat(move1_lost_sc "\emph{Change HS X Score Diff}" , nolabel ) ///
keep(move1_lost_sc move1_win_noncomp_sc) ///
coef(move1_lost_sc "Lost" move1_win_noncomp_sc "Won X Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab A B C D E F G H I J K L M O using table18.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(cbgcontrols N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"1st Stage CBG Controls"' `"Observations"'))


xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc $lotcons $nccons , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store main

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc  $lotcons $nccons if appch1magnet==0 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store A

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc  $lotcons $nccons if appch1magnet==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store B

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc  $lotcons $nccons if appch1ib==1 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store C

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc  $lotcons $nccons if winprob<0.5 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store D

xtreg midbyCBG_price_residV2_diff move1_lost_sc move1_win_noncomp_sc  $lotcons $nccons if winprob>=0.5 , fe cluster(lottery) 
estadd local cbgcontrols = "\checkmark"
estadd scalar N_ind = e(N)
est store E

** table A20
esttab main A B C D E using tableA20.tex , replace ///
mtitles("Full Sample" "Non-Mag." "Magnet" "IB" "<0.5" ">=0.5") ///
refcat(move1_lost_sc "\emph{Change HS X Score Diff}" , nolabel ) ///
keep(move1_lost_sc move1_win_noncomp_sc) ///
coef(move1_lost_sc "Lost" move1_win_noncomp_sc "Won X Non-Complier") ///
label booktabs nonum gaps noobs f collabels(none) ///
star(* 0.10 ** 0.05 *** 0.01) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") ///

esttab main A B C D E using tableA20.tex , append ///
label booktabs nonum gaps noobs f collabels(none) nomtitles plain cells(none) ///
stats(cbgcontrols N_ind , fmt(%1s %9.0f) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"1st Stage CBG Controls"' `"Observations"'))





